<?php
//导航实体类
class NavModel extends Model {
    //private $pid = 0;
    public function __construct() {
        parent::__construct();
        $this->fields = array('id','nav_name','nav_info','sort','pid');
        $this->tables = array(DB_FREFIX.'nav');
        $this->check = new NavCheck();
    }
    public function findFrontNav(){
        $id = $this->input->get('id');
        $where = array("id='$id'");
        if(!$this->check->oneCheck($this,$where)) Redirect::getInstance()->success('./');
        $mainNav = $childNav = $resultNav = array();
        $allNav = parent::select(array('id','nav_name','pid'));
        return Tree::getInstance()->getTree($allNav,$id);
    }
    public function findFrontTenParentNav() {
         return parent::select(array('id','nav_name'),
                       array('where'=>array('pid = 0'),
                             'limit'=>10,
                             'order'=>'sort ASC'));
    }
    public function findAll() {
        $pid = $this->input->get('pid') ? $this->input->get('pid') : 0;
        $where = array("pid='$pid'");
        return parent::select(array('id','nav_name','nav_info','sort'),
                       array('where'=>$where,
                             'limit'=>$this->limit,
                             'order'=>'sort ASC'));
    }
    public function findAllParentNavNoPage(){
        $pid = $this->input->get('id') ? $this->input->get('id') : 0;
        $where = array("pid='$pid'");
        return parent::select(array('id','nav_name'),
                       array('where'=>$where,
                             'order'=>'sort ASC'));
    }
    public function add() {
        $addData = $this->input->post();
        if(!$this->check->addCheck($this,$addData)) die();
        $addData = $this->input->filter($addData,$this->fields);
        $addData['sort'] = $this->nextId();
        return parent::add($addData);
    }
    public function update() {
        $id = $this->input->get('id');
        $where = array("id='$id'");
        if(!$this->check->oneCheck($this,$where)) exit('不存在此数据！');
        $updateData = $this->input->post();
        if(!$this->check->updateCheck($updateData)) die();
        $updateData = $this->input->filter($updateData,$this->fields);
        return parent::update($where,$updateData);
    }
    public function findOne() {
        if(isset($_GET['pid'])){
            $pid = $this->input->get('pid');
            $where = array("id='$pid'");
        } else {
            $id = $this->input->get('id');
            $where = array("id='$id'");
        }
        if(!$this->check->oneCheck($this,$where)) exit('不存在此数据！');
        return parent::select(array('id','nav_name','nav_info'),
                              array('where'=>$where,
                                    'limit'=>'1'));
    }
    public function isOneByAjax() {
        $navName = $this->input->get('nav_name');
        $where = array("nav_name='$navName'");
        return parent::isOne($where);
    }
    public function sort() {
        $sortData = $this->input->post('sort');
        foreach($sortData as $key=>$value){
            if(!is_numeric($value)) continue;
            $where = array("id='$key'");
            parent::update($where,array('sort'=>$value));
        }
        return true;
    }
    public function del() {
        $id = $this->input->get('id');
        $where = array("id='$id'");
        if(!$this->check->oneCheck($this,$where)) exit('不存在此数据！');
        return parent::del($where);
    }
    public function total() {
        $pid = $this->input->get('pid') ? $this->input->get('pid') : 0;
        $where = array("pid='$pid'");
        return parent::total(array('where'=>$where));
    }
}